/*
 * 用户管理模块
 */

import { LoginFormType } from '@/interfaces/LoginType/LoginType'
import { $t } from '@/utils/vue_pbulic'
import { is_user } from '../globalFun'

export interface ResultType {
  user_id: number
  username: string
  token: string | null
}

// 登录
export function Login() {
  return {
    url: 'user/login',
    type: 'post',
    response: (config: LoginFormType): Result<ResultType> => {
      const { username, password } = config
      const get_user = is_user(username)
      if (!get_user) {
        return {
          code: 200,
          message: '游客',
          result: {
            user_id: 999,
            username,
            token: 'token'
          }
        }
      }
      if (password === get_user.password) {
        return {
          code: 200,
          result: {
            user_id: get_user.userId,
            username,
            token: 'token'
          },
          message: $t('login.success')
        }
      }
      return {
        code: 400,
        message: $t('login.error'),
        result: null
      }
    }
  }
}
